package cn.org.cute.ebocy.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 获得随机token
 * @Copyright©2011, 2011 EBO CHANGYOU Co.td. All rights reserved.
 * @Version 1.0.0
 * @since jdk1.6
 * @author xiaolei
 * @date 2012-5-11
 */
public class CreateToken {
	private static String secertKey = "fghijklm2323343456723232wsddfdfgfgfgfgcvbbefrerernopqrstuvwwecdfdfghgh8343434ewe2232323sqwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789yyyerebo3344dsds";
	private static char[] charSet = secertKey.toCharArray();

	private static Random rGen = new Random();

	/**
	 * 产生一个一定数量位数的key
	 * @param codeNum
	 * @return
	 */
	public static String getGenerateTokenKey(int codeNum)
	{
		
		int maxNum = charSet.length;
		 
		if(codeNum > maxNum ){
			System.err.println("只能创建>="+maxNum+"位的字符串.........");
			return null;
		}
	    byte[] rBytes = new byte[codeNum];

	    rGen.nextBytes(rBytes);

	    char[] rName = new char[codeNum];

	    for (int i = 0; i < codeNum; i++){
	        rName[i] = charSet[Math.abs(rBytes[i] % maxNum)];
	    }     
	        
	        

	    String rdstr=new String(rName);

	    return rdstr;

	}
	
	
	/**
	 * 生成32位的key
	 * @return
	 */
	public static String get32GenerateTokenKey(){
		return getGenerateTokenKey(32);
	}
	
	
	public static void main(String[] arg){
		
		long startTime = System.currentTimeMillis();
		List addList = new ArrayList();
		long x = 1;
		long max = x;
		while(x>0){
			x --;
			String tokenKey = CreateToken.getGenerateTokenKey(32);
			//UUID tokenKey = UUID.randomUUID(); 
           if(addList.contains(tokenKey)){
        	   System.out.println("CreateToken--->"+x+"---->"+tokenKey.toString());
				return;
			}
			addList.add(tokenKey);
							
		}
		
		long endTime  = System.currentTimeMillis();
		
		System.out.println("循环"+max+"次用时"+(endTime-startTime)+"毫秒！");
		
	}

	

}
